//
//  ZXZOrderViewController.m
//  sheyingzu
//
//  Created by tyu on 29.5.24.
//

#import "ZXZOrderViewController.h"


#import "ZXZOrderCell.h"

#import "ZXZOrderHireViewController.h"

@interface ZXZOrderViewController ()<UIScrollViewDelegate>


@property(nonatomic,weak) UIScrollView *ScrollView;

@property(nonatomic,weak) UIView *topView;
@property(nonatomic,weak) UITextField *nameTextF;
@property(nonatomic,weak) UITextField *phoneTextF;
@property(nonatomic,weak) UITextField *addressTextF;


@property(nonatomic,weak) UIView *middView;
@property(nonatomic,weak) UIButton *dayBtn1;
@property(nonatomic,weak) UIButton *dayBtn2;
@property(nonatomic,weak) UIButton *dayBtn3;
@property(nonatomic,weak) UIButton *dayBtn4;


@property(nonatomic,weak) UIView *bottomView;

@property(nonatomic,weak) ZXZOrderCell *cell1;
@property(nonatomic,weak) ZXZOrderCell *cell2;
@property(nonatomic,weak) ZXZOrderCell *cell3;
@property(nonatomic,weak) ZXZOrderCell *cell4;
@property(nonatomic,weak) ZXZOrderCell *cell5;

@property(nonatomic,weak) UILabel *sumLab;


@property(nonatomic,copy) NSString *dayStr;
@property(nonatomic,copy) NSString *totalStr;


@end

@implementation ZXZOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


- (void)updata
{
    if ([self.dayStr isEqualToString:@"31"]) {
        self.cell3.subLab.text = @"30天以上";
    }else{
        self.cell3.subLab.text = [NSString stringWithFormat:@"%@天",self.dayStr];
    }
    self.cell5.subLab.text = [NSString stringWithFormat:@"￥%@",self.totalStr];
    self.sumLab.text = [NSString stringWithFormat:@"￥%@",self.totalStr];
}

- (void)bac_addUI
{
    [super bac_addUI];
    [self setupView];
    
    self.bacTitleLabel.text = @"确认订单";
    [self.view bringSubviewToFront:self.navView];
    
    self.dayStr = @"5";
    int num = 150*5+800;
    self.totalStr = [NSString stringWithFormat:@"%d",num];

    [self updata];
    
}
- (void)hireBtnClick
{
    
  
    
  if (self.nameTextF.text.length <=0){
        [NotifyLabel NotifyWithString:@"请输入收件人姓名"];
        return;

    }else if (self.phoneTextF.text.length <=0){
        [NotifyLabel NotifyWithString:@"请输入您的联系方式"];
        return;

    }else if (self.addressTextF.text.length<=3){
        [NotifyLabel NotifyWithString:@"请输入收货地址"];
        return;
    }
    
    ZXZOrderHireViewController *vc = [ZXZOrderHireViewController new];
    vc.totalStr = self.totalStr;
    [self.navigationController pushViewController:vc animated:YES];
    
}



- (void)setupView{
    
    UIScrollView *ScrollV = [[UIScrollView alloc]init];
    [self.view addSubview:ScrollV];
    self.ScrollView =ScrollV;
    ScrollV.delegate = self;

    ScrollV.contentSize = CGSizeMake(ScreenWidth, ScreenHeight*1.1);
    ScrollV.contentInset = UIEdgeInsetsMake(0, 0, -10, 0);
    ScrollV.showsVerticalScrollIndicator = NO;
    ScrollV.frame = CGRectMake(0, 100, ScreenWidth, ScreenHeight-ShowDiff);
    
    [self setTopView];
    [self setupMiddView];
    [self setupBottomView];

    [self setHireBtn];
    
}

- (void)setHireBtn
{
    
    UIView *view = [UIView new];
    [self.view addSubview:view];
    view.backgroundColor = [UIView ColorWithHexString:@"#222222"];
    view.frame = CGRectMake(15, ScreenHeight-twoShowDiff-100, ScreenWidth-30, 100);
    view.layer.cornerRadius = 20;
    view.layer.masksToBounds = YES;
    
    UILabel *bottLab = [self lableWithText:@"租赁费用" andColor:[UIView ColorWithHexString:@"#FFFFFF"] andFont:14];
    [view addSubview:bottLab];
    bottLab.frame = CGRectMake(15, 20, 90, 20);
    
    
    UILabel *sumLab = [self lableWithText:[NSString stringWithFormat:@"￥%@",self.totalStr] andColor:[UIView ColorWithHexString:@"#FFFFFF"] andFont:24];
    [view addSubview:sumLab];
    self.sumLab = sumLab;
    sumLab.frame = CGRectMake(CGRectGetMaxX(bottLab.frame)+10, 20, 90, 20);
    
    
    
    UIButton *dayBtn = [[UIButton alloc] init];
    [view addSubview:dayBtn];
    dayBtn.frame = CGRectMake(ScreenWidth-30-30-100, 20, 100, 60);
    [dayBtn addTarget:self action:@selector(hireBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [dayBtn setTitle:@"立即支付" forState:UIControlStateNormal];
    [dayBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    dayBtn.backgroundColor = [UIView ColorWithHexString:@"#67F3FC"];
    dayBtn.layer.cornerRadius = 10;
    dayBtn.layer.masksToBounds = YES;
    
    
    UILabel *desLab = [self lableWithText:@"含押金￥800，租期结束全额退还" andColor:[UIView ColorWithHexString:@"#07C6D2"] andFont:11];
    [view addSubview:desLab];
    desLab.frame = CGRectMake(15, CGRectGetMaxY(dayBtn.frame)-20, 200, 20);
    
    
}


- (void)setTopView
{
    UILabel *bottLab = [self lableWithText:@"请填写收件人信息" andColor:[UIView ColorWithHexString:@"#333333"] andFont:18];
    [self.ScrollView addSubview:bottLab];
    bottLab.frame = CGRectMake(15, 20, 200, 20);
    
    UIView *view = [UIView new];
    [self.ScrollView addSubview:view];
    self.topView =view;
    view.frame = CGRectMake(15, CGRectGetMaxY(bottLab.frame)+10, ScreenWidth-30, 130);
    view.backgroundColor = [UIView ColorWithHexString:@"#F8F9FB"];
    view.layer.cornerRadius = 10;
    view.layer.masksToBounds = YES;
  
    
    UITextField *nameTextF = [self textfieldPlaceholder:@"收件人姓名" keyboardType:UIKeyboardTypeDefault];
    [view addSubview:nameTextF];
    self.nameTextF =nameTextF;
  
    
    UITextField *phoneTextF = [self textfieldPlaceholder:@"收件人电话" keyboardType:UIKeyboardTypeNumberPad];
    [view addSubview:phoneTextF];
    self.phoneTextF =phoneTextF;
    
    UITextField *addressTextF = [self textfieldPlaceholder:@"收件人地址" keyboardType:UIKeyboardTypeDefault];
    [view addSubview:addressTextF];
    self.addressTextF =addressTextF;
    
    CGFloat width = (ScreenWidth-20-30)/2;
    nameTextF.frame = CGRectMake(10, 10, width, 30);

    phoneTextF.frame = CGRectMake(CGRectGetMaxX(nameTextF.frame), 10, width, 30);
    addressTextF.frame = CGRectMake(10, CGRectGetMaxY(nameTextF.frame)+20, ScreenWidth-20-30, 30);

  
}


- (UITextField *)textfieldPlaceholder:(NSString *)placeholder keyboardType:(UIKeyboardType )keyboardType
{
    UITextField *TextF=[[UITextField alloc]init];
    TextF.borderStyle=UITextBorderStyleNone;
    TextF.placeholder = placeholder;
    TextF.keyboardType = keyboardType;
    return TextF;
}




- (void)setupMiddView
{
    
    
    UILabel *bottLab = [self lableWithText:@"租赁时长" andColor:[UIView ColorWithHexString:@"#333333"] andFont:18];
    [self.ScrollView addSubview:bottLab];
    bottLab.frame = CGRectMake(15, CGRectGetMaxY(self.topView.frame)+20, 80, 20);
    
    UIView *view = [UIView new];
    [self.ScrollView addSubview:view];
    self.middView =view;
    view.frame = CGRectMake(15, CGRectGetMaxY(bottLab.frame)+10, ScreenWidth-30, 50);

    
    for (int i=0; i<4; i++) {
        
        UIButton *dayBtn = [[UIButton alloc] init];
        [view addSubview:dayBtn];
        dayBtn.tag = i+1;
        [dayBtn addTarget:self action:@selector(dayBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [dayBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        dayBtn.backgroundColor = [UIView ColorWithHexString:@"#E8EAEE"];
        dayBtn.layer.cornerRadius = 44/2;
        dayBtn.layer.masksToBounds = YES;
        
        if (i==0) {
            [dayBtn setTitle:@"5天" forState:UIControlStateNormal];
            dayBtn.backgroundColor = [UIColor blackColor];
            [dayBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.dayBtn1 = dayBtn;
        }else if (i==1){
            [dayBtn setTitle:@"15天" forState:UIControlStateNormal];
            self.dayBtn2 = dayBtn;

        }else if (i==2){
            [dayBtn setTitle:@"30天" forState:UIControlStateNormal];
            self.dayBtn3 = dayBtn;

        }else if (i==3){
            [dayBtn setTitle:@"30天以上" forState:UIControlStateNormal];
            self.dayBtn4 = dayBtn;

        }
        
    }
    
    CGFloat width = (ScreenWidth-(5*15))/4 -20;
    
    self.dayBtn1.frame = CGRectMake(15, 5,width, 44);
    self.dayBtn2.frame = CGRectMake(CGRectGetMaxX(self.dayBtn1.frame) +15, 5,width, 44);
    self.dayBtn3.frame = CGRectMake(CGRectGetMaxX(self.dayBtn2.frame) +15, 5,width, 44);
    self.dayBtn4.frame = CGRectMake(CGRectGetMaxX(self.dayBtn3.frame) +15, 5,width+30, 44);
  

}

- (void)dayBtnClick:(UIButton*)button
{
    
    [self updataBtn:self.dayBtn1];
    [self updataBtn:self.dayBtn2];
    [self updataBtn:self.dayBtn3];
    [self updataBtn:self.dayBtn4];

    button.backgroundColor = [UIColor blackColor];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    int num = 5;
    if(button.tag == 1){
        self.dayStr = @"5";
        num = 5;
    }else if (button.tag == 2){
        self.dayStr = @"15";
        num = 15;

    }else if (button.tag == 3){
        self.dayStr = @"30";
        num = 30;

    }else if (button.tag == 4){
        self.dayStr = @"31";
        num = 25;

    }
    
    
    int tatal = 150*num+800;
    self.totalStr = [NSString stringWithFormat:@"%d",tatal];

    [self updata];
    
    
    
}


- (void)updataBtn:(UIButton *)dayBtn
{
    [dayBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    dayBtn.backgroundColor = [UIView ColorWithHexString:@"#E8EAEE"];
    
}

- (void)setupBottomView
{
    
    UILabel *bottLab = [self lableWithText:@"订单详情" andColor:[UIView ColorWithHexString:@"#333333"] andFont:18];
    [self.ScrollView addSubview:bottLab];
    bottLab.frame = CGRectMake(15, CGRectGetMaxY(self.middView.frame)+20, 80, 20);
    
    
    UIView *view = [UIView new];
    [self.ScrollView addSubview:view];
    self.bottomView =view;
    view.frame = CGRectMake(15, CGRectGetMaxY(bottLab.frame)+10, ScreenWidth-30, 220);
    view.backgroundColor = [UIView ColorWithHexString:@"#F8F9FB"];
    view.layer.cornerRadius = 10;
    view.layer.masksToBounds = YES;
    
    
    for (int i=0; i<6; i++) {
        ZXZOrderCell *cell = [ZXZOrderCell new];
        [view addSubview:cell];
        
        if (i==0) {
            cell.TitleLab.text = @"商品单价";
            cell.subLab.text = @"￥150";
            self.cell1 = cell;
        }else if (i==1){
            
                cell.TitleLab.text = @"押金";
                cell.subLab.text = @"￥800";
                self.cell2 = cell;

        }else if (i==2){
            
                cell.TitleLab.text = @"租赁天数";
                cell.subLab.text = @"15";
                self.cell3 = cell;
        }else if (i==3){
            
            cell.TitleLab.text = @"运费";
            cell.subLab.text = @"￥0";
            self.cell4 = cell;
        }else if (i==4){
            
            cell.TitleLab.text = @"";
            cell.subLab.text = [NSString stringWithFormat:@"￥%@",self.totalStr];
            cell.subLab.font = [UIFont boldSystemFontOfSize:20];
            cell.subNumLab.hidden = NO;
            cell.backgroundColor = [UIView ColorWithHexString:@"#67F3FC"];
            self.cell5 = cell;
            self.cell5.layer.cornerRadius = 10;
            self.cell5.layer.masksToBounds = YES;

        }
    }
    CGFloat width = ScreenWidth-30;
    CGFloat height = 40;

    self.cell1.frame = CGRectMake(0, 0, width, height);
    self.cell2.frame = CGRectMake(0, CGRectGetMaxY(self.cell1.frame), width, height);
    self.cell3.frame = CGRectMake(0, CGRectGetMaxY(self.cell2.frame), width, height);
    self.cell4.frame = CGRectMake(0, CGRectGetMaxY(self.cell3.frame), width, height);
    self.cell5.frame = CGRectMake(0, CGRectGetMaxY(self.cell4.frame)+10, width, 40);
    
    
}




- (UILabel *)lableWithText:(NSString *)title andColor:(UIColor *)color andFont:(CGFloat )font
{
    
    UILabel *rightTitle  = [[UILabel alloc]init];
    rightTitle.font = [UIFont systemFontOfSize:font];
    rightTitle.textColor = color;
    rightTitle.text = title;
    return rightTitle;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
